package com.jbp.controller;

import com.jbp.exception.RRException;
import com.jbp.model.SkillOrder;
import com.jbp.service.SkillOrderService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 技能订单(SkillOrder)表控制层
 * @author cc
 * @since 2020-11-28 17:49:31
 */
@Api(tags ="APP接口--SkillOrder" )
@RestController
@RequestMapping("rest/skillOrder")
public class SkillOrderController {
    @Autowired
    private SkillOrderService skillOrderService;

    @ApiOperation(value="我的下单")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = SkillOrder.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/buyList")
    public R buyList(@RequestParam Map<String, Object> params, @RequestAttribute("userId") Integer userId){
        PageUtils page = skillOrderService.buyList(params, userId);
        return R.ok(page);
    }

    @ApiOperation(value="我的接单")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = SkillOrder.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/sellList")
    public R sellList(@RequestParam Map<String, Object> params, @RequestAttribute("userId") Integer userId){
        PageUtils page = skillOrderService.sellList(params, userId);
        return R.ok(page);
    }

    @ApiOperation(value="进行中的订单")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = SkillOrder.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/goingOrder")
    public R goingOrder(@RequestParam ("otherId") Integer otherId, @RequestAttribute("userId") Integer userId){
        List<SkillOrder> list = skillOrderService.goingOrder(otherId, userId);
        return R.ok(list);
    }
    
    /** 创建订单 */
    @ApiOperation(value="创建订单")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = SkillOrder.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/create")
    public R create(@RequestBody SkillOrder skillOrder, @RequestAttribute("userId") Integer userId){
        if (skillOrder.getSkillUserId()==null || skillOrder.getServerTime()==null
                || skillOrder.getTimes()==null || skillOrder.getTimes()<=0) {
            throw new RRException("参数错误");
        }
        return R.ok(skillOrderService.create(skillOrder, userId));
    }

    /** 创建订单 */
    @ApiOperation(value="创建订单")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = SkillOrder.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/pay")
    public R pay(@RequestBody SkillOrder skillOrder, @RequestAttribute("userId") Integer userId){
        if (skillOrder.getId()==null) {
            throw new RRException("参数错误");
        }
        skillOrderService.pay(skillOrder, userId);
        return R.ok();
    }

    @ApiOperation(value="确认订单 大神端")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/confirm")
    public R confirm(@RequestBody SkillOrder skillOrder, @RequestAttribute("userId") Integer userId){
        if (skillOrder.getId()==null) {
            throw new RRException("参数错误");
        }
        skillOrderService.confirm(skillOrder, userId);
        return R.ok();
    }

    @ApiOperation(value="开始服务 大神端")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/start")
    public R start(@RequestBody SkillOrder skillOrder, @RequestAttribute("userId") Integer userId){
        if (skillOrder.getId()==null) {
            throw new RRException("参数错误");
        }
        skillOrderService.start(skillOrder, userId);
        return R.ok();
    }

    @ApiOperation(value="完成订单 客户端")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/complete")
    public R complete(@RequestBody SkillOrder skillOrder, @RequestAttribute("userId") Integer userId){
        if (skillOrder.getId()==null) {
            throw new RRException("参数错误");
        }
        skillOrderService.complete(skillOrder, userId);
        return R.ok();
    }

    @ApiOperation(value="取消订单 客户端大神端")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/cancle")
    public R cancle(@RequestBody SkillOrder skillOrder, @RequestAttribute("userId") Integer userId){
        if (skillOrder.getId()==null) {
            throw new RRException("参数错误");
        }
        skillOrderService.cancle(skillOrder, userId);
        return R.ok();
    }

    @ApiOperation(value="订单详情")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功", response = SkillOrder.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/detail")
    public R detail(@RequestParam Integer skillOrderId){
        if (skillOrderId==null) {
            throw new RRException("参数错误");
        }
        return R.ok(skillOrderService.detail(skillOrderId));
    }
}